<?php
namespace XtwsBack\Controller;
use Think\Controller;
class SpellgroupController extends Controller{
    public function Index(){
        $member = M('member');
        $spellgroup = M('shop_spellgroup');
		
		$where="";
		if($_GET['keyword']!=''){
			$where=" and title like '%".$_GET['keyword']."%'";
			$this->assign('keyword',$_GET['keyword']);// 赋值分页输出
		}

        $count = $spellgroup->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,100);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $spellgroup_info =$spellgroup->where('state <> 3 '.$where)->order('state,id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($spellgroup_info as $k => $v){
            $username = $member->where('id='.$v['u_id'])->field('nickname')->find();
            $spellgroup_info[$k]['username'] = $username['nickname'];
            //清除为零的时间戳
            if ($v['starttime'] == 0){
                $spellgroup_info[$k]['starttime'] = '';
            }
            if ($v['endtime'] == 0){
                $spellgroup_info[$k]['endtime'] = '';
            }
            switch ($v['state']){
                case 0:
                    $spellgroup_info[$k]['state'] = '待审';
                    break;
                case 1:
                    $spellgroup_info[$k]['state'] = '通过';
                    break;
                case 2:
                    $spellgroup_info[$k]['state'] = '不通过';
                    break;
                case 3:
                    $spellgroup_info[$k]['state'] = '已删除';
                    break;
                case 4:
                    $spellgroup_info[$k]['state'] = '已结束';
                    break;
            }
        }
        $this->assign('list',$spellgroup_info);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }//end fun index

    public function Addgroup(){
        $member = M('member');
        $category = M('category');
        $store = M('shop_store');
        $spellgroup = M('shop_spellgroup');
        //获取相关信息
        $getfirst=M('area')->where("`re_id` =0")->select();//获取一级所有地址
        $list_category = $category->where('pid = 22 AND status = 1')->field('id,title')->select();//获取单位规格下的单位分类
        $type_category = $category->where('pid = 31 AND status = 1')->field('id,title')->select();//获取选择美味下的分类
        $list_member = $member->field('id,u_type,nickname')->select();//获取用户列表
        $list_store = $store->field('id,storename')->select();//获取店铺列表
        //添加拼团订单到数据库
        $post_group = I("post.");
        if($post_group){
//            dump($post_group);die;
            $member_type = getMember($post_group['u_id']);
            $post_group['type'] = $member_type['u_type'] == 2 ? 0 : 1;//区分农户门店发货
            $post_group['starttime'] = strtotime($post_group['starttime']);
            $post_group['endtime'] = strtotime($post_group['endtime']);
            $post_group['addtime'] = time();

            if($post_group['c_imgs']){//处理详情的图片
                // 将字符串打散为数组 http://imgs.xiangtws.com/Public/Uploads/5b0e429f3d36b.jpg,http://imgs.xiangtws.com/Public/Uploads/5b0e429f3d36b.jpg
                $imgs_str = explode(',',$post_group['c_imgs']);
                // 将路径中拼接上img标签
                foreach ($imgs_str as $k=>$v){
                    $img_arr[$k] = '<img src="'.$v.'">';
                }
                $post_group['content'] = implode("", $img_arr);
            }//end if
            $addgroup = $spellgroup->add($post_group);
            if ($addgroup){
                // 处理批发价
                if($post_group['start']!=''&&$post_group['end']!=''&&$post_group['p_price']!=''&&$post_group['type']==0){
                    // 处理数组
                    foreach ($post_group['start'] as $k=>$v){
                        foreach ($post_group['end'] as $k2=>$v2){
                            $data[$k2]['end'] = $v2;
                        }
                        foreach ($post_group['p_price'] as $k3=>$v3){
                            $data[$k3]['price'] = $v3;
                        }
                        $data[$k]['start'] = $v;
                        $data[$k]['sg_id'] = $addgroup;
                    }//end foreach
                    // 添加数据
                    foreach ($data as $v){
                        M('spellgroup_price_range')->add($v);
                    }
                }//end if
                $this->success('添加成功','index.html');
            }else{
                $this->error('添加失败');
            }//end if
        }//end if
        $this->assign('areali',$getfirst);
        $this->assign('list_member',$list_member);
        $this->assign('type_category',$type_category);
        $this->assign('list_category',$list_category);
        $this->assign('list_store',$list_store);
        $this->display();
    }//end fun Add

    public function Savegroup(){
        $member = M('member');
        $category = M('category');
        $store = M('shop_store');
        $spellgroup = M('shop_spellgroup');
        //获取相关信息
        $getfirst=M('area')->where("`re_id` =0")->select();//获取一级所有地址
        $list_category = $category->where('pid = 22 AND status = 1')->field('id,title')->select();//获取单位规格下的单位分类
        $type_category = $category->where('pid = 31 AND status = 1')->field('id,title')->select();//获取选择美味下的分类
        $list_member = $member->field('id,nickname')->select();//获取用户列表
        $list_store = $store->field('id,storename')->select();//获取店铺列表
        $id = $_GET['id']?$_GET['id']:I('id');
        if($id){
            $spellgroup_info = $spellgroup->where('id='.$id)->find();
			//首页图分离
			$spellgroup_info['h_img']=explode(",",$spellgroup_info['h_imgs']);
			//详情图片分离
			$spellgroup_info['c_img']=explode(",",$spellgroup_info['c_imgs']);

            $spellgroup_info['starttime'] = date('Y-m-d H:i:s',$spellgroup_info['starttime']);
            $spellgroup_info['endtime'] = date('Y-m-d H:i:s',$spellgroup_info['endtime']);

            //批发价
            $spellgroup_info['price_range'] = M('spellgroup_price_range')->where('sg_id='.$id)->order('id asc')->select();
            //区分门店和农户
            if ($spellgroup_info['type'] == '0'){
                $member = getMember($spellgroup_info['u_id']);//获取农户的信息
                $farmers = M("member_farmers")->where("u_id = {$spellgroup_info['u_id']}")->find();
                $farmers_info = array(
                    'name' => $member['realname'],
                    'address' => getarea($farmers['a_id_1']).getarea($farmers['a_id_2']).getarea($farmers['a_id_3']).getarea($farmers['a_id_4']).getarea($farmers['a_id_5']).$farmers['village']
                );
            }//end if
//            dump($spellgroup_info);
            $post_group = $_POST;
//            dump($post_group);die;
            if ($post_group){
                $post_group['starttime'] = strtotime($post_group['starttime']);
                $post_group['endtime'] = strtotime($post_group['endtime']);
                $post_group['uptime'] = time();
                foreach($post_group as $k => $v){
                    if ($k == 'a_id_1' || $k == 'a_id_2'){
                        if ($v == 0){
                            unset($post_group[$k]);
                        }
                    }//end if
                    if ($v == ''){
                        if ($k != 'content'){
                            unset($post_group[$k]);
                        }
                    }//end if
                }//end foreach
                $addgroup = $spellgroup->where('id ='.$id)->save($post_group);
                if ($addgroup){
                    // 处理批发价
                    if($post_group['start']!=''&&$post_group['end']!=''&&$post_group['p_price']!=''){
                        // 处理数组
                        foreach ($post_group['start'] as $k=>$v){
                            foreach ($post_group['p_id'] as $k1=>$v1){
                                $data[$k1]['id'] = $v1;
                            }//end foreach
                            foreach ($post_group['end'] as $k2=>$v2){
                                $data[$k2]['end'] = $v2;
                            }//end foreach
                            foreach ($post_group['p_price'] as $k3=>$v3){
                                $data[$k3]['price'] = $v3;
                            }//end foreach
                            $data[$k]['start'] = $v;
                        }//end foreach
                        // 修改
                        foreach ($data as $v){
                            M('spellgroup_price_range')->save($v);
                        }//end foreach
                    }//end if
                    $this->success('添加成功','index.html');
                    return;
                }else{
                    $this->error('添加失败');
                }//end if
            }//end if
            $this->assign($farmers_info);
            $this->assign('areali',$getfirst);
            $this->assign('list_member',$list_member);
            $this->assign('list_category',$list_category);
            $this->assign('type_category',$type_category);
            $this->assign('list_store',$list_store);
            $this->assign('spellgroup',$spellgroup_info);
            $this->display();
        }else{
            $this->error('参数错误');
        }//end if
    }//end fun Save
    public function Deletegroup(){
        $spellgroup = M('shop_spellgroup');
        $id = I('id');
        if ($id){
            $delegroup = $spellgroup->where('id='.$id)->save(['state' => 3]);
            if ($delegroup){
                $this->success('删除成功','index.html');
            }else{
                $this->error('删除失败');
            }
        }
    }//end fun Delete
}//end class